/**
 * Folgende Variablen werden im entsprechenden Viewscript initialisiert.
 * - availabilityInfo
 * - productInfo
 * - imagesInfo
 * - imageVariantsInfo
 * - productImagePath
 * - thumbnailImagePath
 * - baseUrl
 *
 * Zusätzlich werden folgende HTML Formular-Elemente als Datenbehälter
 * eingesetzt und im folgenden Skript über ihre entsprechenden IDs angesprochen.
 * - productId
 * - quantity
 * - price
 * - currencyCode
 * - sizeId
 * - colorId
 *
 * @see: sherpa/views/scripts/productdetails/index.phtml
 */
$(document).ready(function() {

	// Initialisierung der Tabs
	setTabAttr();

	/**
	 * Event: Tabs für Produktdetails
	 */
	$('#prd-details ul a').click(function() {
		$('#prd-details li[class=active] a').parent().removeAttr('class');
		$(this).parent().attr('class', 'active');
		setTabAttr();
		return false;
	});

	/**
	 * Event: In den Warenkorb
	 */
	$('#btn-addtocart').click(function() {
		var url = baseUrl + 'sherpa/api/cart/add/';
		var getvars = {
			'productId': $('#productId').val(),
			'quantity': $('#quantity option:selected').val()
		};
		// AJAX Request
		$.get(url, getvars, function(data) {
			refreshCart();
		});
		return false;
	});

	// Initialisierung der Produktefarben/-grössen
	init();

	/**
	 * Event: Farbe (mouseover)
	 */
	$('#prd-controls-colors a').mouseover(function() {
		var parts = $(this).attr('id').split('-');
		var colorId = parts[1];
		setSizeAvailability(colorId);
	});

	/**
	 * Event: Farbe (mouseout)
	 */
	$('#prd-controls-colors a').mouseout(function() {
		setSizeAvailability($('#colorId').val());
	});

	/**
	 * Event: Farbe (clicked)
	 */
	$('#prd-controls-colors a').click(function() {
		var parts = $(this).attr('id').split('-');
		var colorId = parts[1];
		$('#colorId').val(colorId);
		setSelectedColor(colorId);
		swapImages();
		return false;
	});

	/**
	 * Event: Grösse (mouseover)
	 */
	$('#prd-controls-sizes a').mouseover(function() {
		var parts = $(this).attr('id').split('-');
		var sizeId = parts[1];
		setColorAvailability(sizeId);
	});

	/**
	 * Event: Grösse (mouseout)
	 */
	$('#prd-controls-sizes a').mouseout(function() {
		setColorAvailability($('#sizeId').val());
	});

	/**
	 * Event: Grösse angeklickt
	 */
	$('#prd-controls-sizes a').click(function() {
		var parts = $(this).attr('id').split('-');
		var sizeId = parts[1];
		$('#sizeId').val(sizeId);
		setSelectedSize(sizeId);
		return false;
	});

	/**
	 * Event: Bildbrowser, Thumbnail-Bild angeklickt
	 */
	$('.image-browser img').click(function() {
		var parts = $(this).attr('id').split('-');
		$('#imageId').val(parts[1]);
		$('.image-browser img').removeAttr('class');
		$(this).attr('class', 'selected');
		swapImages();
		return false;
	});

});

/**
 * Initialisierung: Zeige das erste verfügbare Produkt an (available=1)
 */
function init()
{
	if(productInfo.length > 0) {
		for(i=0; i<productInfo.length; i++) {
			if(productInfo[i][5] == 1) {
				$('#productId').val(productInfo[i][0]);
				var colorId = productInfo[i][1];
				$('#colorId').val(colorId);
				var sizeId = productInfo[i][2];
				$('#sizeId').val(sizeId);
				setColorAvailability(sizeId);
				setSizeAvailability(colorId);
				setSelectedColor(colorId);
				setSelectedSize(sizeId);
				swapImages();
				break;
			}
		}
	}
}

/**
 * Setze Tab-Attribute
 */
function setTabAttr()
{
	$('#prd-details-tabs div div').attr('style', 'display:none');
	var tabTextId = $("#prd-details li[class=active] a").attr('href');
	$(tabTextId).attr('style', 'display:block');
}

/**
 * Setze Produktinfos
 */
function _setProductInfo()
{
	var availabilityTextValue = '';
	if(productInfo.length > 0) {
		var currentColorId = $('#colorId').val();
		var currentSizeId = $('#sizeId').val();
		for(i=0; i<productInfo.length; i++) {
			var colorId = productInfo[i][1];
			var sizeId = productInfo[i][2];
			var available = productInfo[i][5];
			var availabilityText = productInfo[i][6];
			if((sizeId == currentSizeId) && (colorId == currentColorId)) {
				if((available == 0) && (availabilityText != '')) {
					availabilityTextValue = availabilityText;
					break;
				} else {
					var addToCart = $('#btn-addtocart');
					addToCart.removeAttr('disabled');
					addToCart.css('cursor', 'pointer');
					addToCart.attr('alt', 'In den Warenkorb');
					addToCart.attr('title', 'In den Warenkorb');
					addToCart.attr('src', 'sherpa/img/btn-in-den-warenkorb.gif');
					$('#price').val(productInfo[i][3]);
					$('#productId').val(productInfo[i][0]);
					$('#prd-controls-availabilityText').html('');
					return;
				}
			}
		}
	}
	var addToCart = $('#btn-addtocart');
	addToCart.attr('disabled', 'disabled');
	addToCart.css('cursor', 'default');
	addToCart.attr('alt', 'Nicht verfügbar');
	addToCart.attr('title', 'Nicht verfügbar');
	addToCart.attr('src', 'sherpa/img/btn-nicht-verfuegbar.gif');
	$('#prd-controls-availabilityText').html(availabilityTextValue);
	$('#price').val(0);
	$('#productId').val(0);
}

/**
 * Ausgewählte (=angeklickte) Grösse setzen
 *
 * @param integer paramSizeId
 * @return void
 */
function setSelectedSize(paramSizeId)
{
	var sizeId = 0;
	var sizeElementId = '';
	for(i=0; i<availabilityInfo.sizes.length; i++) {
		sizeId = availabilityInfo.sizes[i];
		sizeElementId = '#sizeid-' + sizeId;
		if(sizeId == paramSizeId) {
			$(sizeElementId).attr('class', 'selected');
		} else {
			$(sizeElementId).removeAttr('class');
		}
	}
	_setProductInfo();
}

/**
 * Ausgewählte (=angeklickte) Farbe setzen
 *
 * @param integer paramColorId
 * @return void
 */
function setSelectedColor(paramColorId)
{
	var colorId = '';
	var colorElementId = '';
	for(i=0; i<availabilityInfo.colors.length; i++) {
		colorId = availabilityInfo.colors[i];
		colorElementId = '#colorid-' + colorId;
		if(colorId == paramColorId) {
			$(colorElementId).attr('class', 'colorid selected');
		} else {
			$(colorElementId).attr('class', 'colorid');
		}
	}
	_setProductInfo();
}

/**
 * Setze Verfügbarkeiten der Grössen
 *
 * @param integer colorId
 * @return void
 */
function setSizeAvailability(colorId)
{
	if(availabilityInfo.colorToSize[colorId]) {
		var currentClass = '';
		for(i=0; i<availabilityInfo.sizes.length; i++) {
			var sizeId = availabilityInfo.sizes[i];
			var sizeElementId = '#sizeid-' + sizeId;
			currentClass = $(sizeElementId).attr('class');
			if(availabilityInfo.colorToSize[colorId][sizeId] == 0) {
				if(currentClass != '') {
					$(sizeElementId).attr('class', 'selected unavailable');
				} else {
					$(sizeElementId).removeAttr('class');
				}
			} else {
				if((currentClass == 'selected unavailable') || (currentClass == 'selected')) {
					$(sizeElementId).attr('class', 'selected');
				} else {
					$(sizeElementId).removeAttr('class');
				}
			}
		}
	}
}

/**
 * Setze  Verfügbarkeiten der Farben
 *
 * @param integer sizeId
 * @return void
 */
function setColorAvailability(sizeId)
{
	if(availabilityInfo.sizeToColor[sizeId]) {
		for(i=0; i<availabilityInfo.colors.length; i++) {
			var colorId = availabilityInfo.colors[i];
			var colorElementId = '#colorid-' + colorId;
			if(availabilityInfo.sizeToColor[sizeId][colorId] == 0) {
				$(colorElementId + " img").css('display', 'inline');
			} else {
				$(colorElementId + " img").css('display', 'none');
			}
		}
	}
}

/**
 * Wechsle das Produktbild und die Thumnail-Bilder
 */
function swapImages()
{
	/*
	var url = baseUrl + 'sherpa/api/loadimage/';
	var vars = {
		'imageId': $('#imageId').val(),
		'colorApplicationId': $('#colorId').val()
	};
	$.getJSON(url, vars, function(json) {
		if(json.fileName) {
			$('#product-image').attr('src', productImagePath + json.fileName);
			// Thumbnail
			var thumbnailId = 'imageid-' + $('#imageId').val();
			$('.image-browser #' + thumbnailId).attr('src', thumbnailImagePath + json.fileName);
		}
	});
	*/

	var imageId = $('#imageId').val();
	var colorId = $('#colorId').val();
	var thumbnailId = 0;

	// Produktbild austauschen
	for(i=0; i<imageVariantsInfo.length; i++) {
		if(imageId == imageVariantsInfo[i][0]) {
			if(colorId == imageVariantsInfo[i][1]) {
				$('#product-image').attr('src',
					productImagePath + imageVariantsInfo[i][2]
				);
				break;
			}
		}
	}

	// Thumbnails aller Produktebilder austauschen
	for(i=0; i<imagesInfo.length; i++) {
		imageId = imagesInfo[i][0];
		for(j=0; j<imageVariantsInfo.length; j++) {
			if(imageId == imageVariantsInfo[j][0]) {
				if(colorId == imageVariantsInfo[j][1]) {
					thumbnailId = 'imageid-' + imageId;
					$('.image-browser #' + thumbnailId).attr(
						'src',
						thumbnailImagePath + imageVariantsInfo[j][2]
					);
					break;
				}
			}
		}
	}

}